<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="162px"
    >
      <el-form-item label="公司名称" prop="companyName">
        <el-input
          v-model="dataForm.companyName"
          placeholder="公司名称"
        ></el-input>
      </el-form-item>
      <el-form-item label="公司地址" prop="companyAddress">
        <el-input
          v-model="dataForm.companyAddress"
          placeholder="公司地址"
        ></el-input>
      </el-form-item>
      <el-form-item label="公司所在行政区划编码" prop="adminDivisionitemId">
        <el-input
          v-model="dataForm.adminDivisionitemId"
          placeholder="公司所在行政区划编码"
        ></el-input>
      </el-form-item>
      <el-form-item label="公司类型" prop="typeId">
        <!-- <el-input v-model="dataForm.typeId" placeholder="公司类型id"></el-input> -->
        <el-select
          style="width:100%"
          v-model="dataForm.typeId"
          placeholder="请选择公司类型"
        >
          <el-option
            v-for="item in options"
            :key="item.key"
            :label="item.value"
            :value="item.key"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="统一社会信用代码" prop="orgCode">
        <el-input
          v-model="dataForm.orgCode"
          placeholder="统一社会信用代码"
        ></el-input>
      </el-form-item>
      <el-form-item label="成立时间" prop="establishDate">
        <!-- <el-input v-model="dataForm.establishDate" placeholder="成立时间"></el-input> -->
        <el-date-picker
          style="width:100%"
          value-format="yyyy-MM-dd HH:mm:ss"
          format="yyyy-MM-dd HH:mm:ss"
          v-model="dataForm.establishDate"
          type="datetime"
          placeholder="选择日期"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="公司邮箱" prop="email">
        <el-input v-model="dataForm.email" placeholder="公司邮箱"></el-input>
      </el-form-item>
      <el-form-item label="联系电话" prop="contactPhone">
        <el-input
          v-model="dataForm.contactPhone"
          placeholder="联系电话"
        ></el-input>
      </el-form-item>
      <el-form-item label="法定代表" prop="legalUser">
        <el-input
          v-model="dataForm.legalUser"
          placeholder="法定代表"
        ></el-input>
      </el-form-item>
      <el-form-item label="法人身份证号" prop="legalUserIdNumber">
        <el-input
          v-model="dataForm.legalUserIdNumber"
          placeholder="法人身份证号"
        ></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { isEmail, isIdCard } from "@/utils/validate";
export default {
  data() {
    var validateEmail = (rule, value, callback) => {
      if (!isEmail(value)) {
        callback(new Error("邮箱格式错误"));
      } else {
        callback();
      }
    };
    var validateisIdCard = (rule, value, callback) => {
      if (!isIdCard(value)) {
        callback(new Error("身份证号码格式错误"));
      } else {
        callback();
      }
    };
    return {
      options: [],
      visible: false,
      dataForm: {
        id: 0,
        companyName: "",
        companyAddress: "",
        adminDivisionitemId: "",
        typeId: "",
        orgCode: "",
        establishDate: "",
        email: "",
        contactPhone: "",
        legalUser: "",
        legalUserIdNumber: ""
      },
      dataRule: {
        companyName: [
          { required: true, message: "公司名称不能为空", trigger: "blur" }
        ],
        companyAddress: [
          { required: true, message: "公司地址不能为空", trigger: "blur" }
        ],
        adminDivisionitemId: [
          {
            required: true,
            message: "公司所在行政区划编码不能为空",
            trigger: "blur"
          }
        ],
        typeId: [
          { required: true, message: "公司类型id不能为空", trigger: "blur" }
        ],
        orgCode: [
          {
            required: true,
            message: "统一社会信用代码不能为空",
            trigger: "blur"
          }
        ],
        // establishDate: [
        //   { required: true, message: '成立时间不能为空', trigger: 'blur' }
        // ],
        email: [
          { required: true, message: "公司邮箱不能为空", trigger: "blur" },
          { validator: validateEmail, trigger: "blur" }
        ],
        contactPhone: [
          { required: true, message: "联系电话不能为空", trigger: "blur" }
        ],
        legalUser: [
          { required: true, message: "法定代表不能为空", trigger: "blur" }
        ],
        legalUserIdNumber: [
          { required: true, message: "法人身份证号不能为空", trigger: "blur" },
          { validator: validateisIdCard, trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    // 获取公司类型
    getCompanyTypes() {
      this.$http({
        url: this.$http.adornUrl(`/sys/getParamenters/getCompanyType`),
        method: "get"
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.options = data.list;
        }
      });
    },
    init(id) {
      this.getCompanyTypes();
      this.dataForm.id = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/sys/bcompany/info/${this.dataForm.id}`),
            method: "get",
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.companyName = data.bCompany.companyName;
              this.dataForm.companyAddress = data.bCompany.companyAddress;
              this.dataForm.adminDivisionitemId =
                data.bCompany.adminDivisionitemId;
              this.dataForm.typeId = data.bCompany.typeId;
              this.dataForm.orgCode = data.bCompany.orgCode;
              this.dataForm.establishDate = data.bCompany.establishDate;
              this.dataForm.email = data.bCompany.email;
              this.dataForm.contactPhone = data.bCompany.contactPhone;
              this.dataForm.legalUser = data.bCompany.legalUser;
              this.dataForm.legalUserIdNumber = data.bCompany.legalUserIdNumber;
            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/sys/bcompany/${!this.dataForm.id ? "save" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              companyName: this.dataForm.companyName,
              companyAddress: this.dataForm.companyAddress,
              adminDivisionitemId: this.dataForm.adminDivisionitemId,
              typeId: this.dataForm.typeId,
              orgCode: this.dataForm.orgCode,
              establishDate: this.dataForm.establishDate,
              email: this.dataForm.email,
              contactPhone: this.dataForm.contactPhone,
              legalUser: this.dataForm.legalUser,
              legalUserIdNumber: this.dataForm.legalUserIdNumber
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                }
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    }
  }
};
</script>
